package com.showde.shoppingmall.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.domain.SearchResult;

public class SearchResultAdapter extends BaseAdapter {

	private Context context;
	List<SearchResult> searchResults;
	ImageLoader imageLoader;

	public SearchResultAdapter(Context context, List<SearchResult> searchResults) {
		this.context = context;
		this.searchResults = searchResults;
		imageLoader = new ImageLoader(context);
	}

	@Override
	public int getCount() {
		return searchResults.size();
	}

	@Override
	public SearchResult getItem(int position) {
		return searchResults.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_product_search, null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.img_product_icon);
			holder.descTextView = (TextView) convertView
					.findViewById(R.id.txt_pro_desc);
			holder.priceTextView = (TextView) convertView
					.findViewById(R.id.txt_pro_price);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		SearchResult result = getItem(position);
		imageLoader.displayImage(result.pLogo, holder.imageView);
		holder.descTextView.setText(result.pDesc);
		holder.priceTextView.setText("单价：￥" + result.pPrice);
		return convertView;
	}

	private final class ViewHolder {
		ImageView imageView;
		TextView descTextView;
		TextView priceTextView;
	}
}
